package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.common.converter;

import com.fasterxml.jackson.databind.JsonNode;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.ServiceTaskTypeEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.event.NodeEventExecuter;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.ExternalApiDelegate;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.SpringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import org.flowable.bpmn.model.*;
import org.flowable.editor.language.json.converter.BaseBpmnJsonConverter;
import org.flowable.editor.language.json.converter.BpmnJsonConverterUtil;
import org.flowable.editor.language.json.converter.ServiceTaskJsonConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.Beans;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: niechanggang
 * @date:2021/8/6 15:53  服务节点 Json解析器
 * @description:
 */
public class CustomServiceTaskJsonConverter  extends ServiceTaskJsonConverter {

    private   static Logger  logger  = LoggerFactory.getLogger(CustomServiceTaskJsonConverter.class);

    public static void fillTypes(Map<String, Class<? extends BaseBpmnJsonConverter>> convertersToBpmnMap, Map<Class<? extends BaseElement>, Class<? extends BaseBpmnJsonConverter>> convertersToJsonMap) {

        fillJsonTypes(convertersToBpmnMap);
        fillBpmnTypes(convertersToJsonMap);
    }

    public static void fillJsonTypes(Map<String, Class<? extends BaseBpmnJsonConverter>> convertersToBpmnMap) {
        convertersToBpmnMap.put(STENCIL_TASK_SERVICE, CustomServiceTaskJsonConverter.class);
    }

    public static void fillBpmnTypes(Map<Class<? extends BaseElement>, Class<? extends BaseBpmnJsonConverter>> convertersToJsonMap) {
        convertersToJsonMap.put(ServiceTask.class, CustomServiceTaskJsonConverter.class);
    }

    @Override
    protected FlowElement convertJsonToElement(JsonNode elementNode, JsonNode modelNode, Map<String, JsonNode> shapeMap) {
        ServiceTask task = new ServiceTask();
        logger.info("ElementNode:"+elementNode);
        JsonNode  propsNode = elementNode.get("properties");
        JsonNode serviceConf =  propsNode.get("serviceConf");
        String type  =  BpmnJsonConverterUtil.getValueAsString("type", serviceConf);
        String expression  = BpmnJsonConverterUtil.getValueAsString("expression", serviceConf);
        String  resultVariable  =  BpmnJsonConverterUtil.getValueAsString("resultVariable", serviceConf);
        String  failedJobRetryTimeCycleValue  =  BpmnJsonConverterUtil.getValueAsString("failedJobRetryTimeCycleValue", serviceConf);
        if(ServiceTaskTypeEnum.JAVA_DELEGATE.getCode().equals(type)){
            task.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_CLASS);
            task.setImplementation(expression);
        }else if(ServiceTaskTypeEnum.DELEGATE_EXPRESSION.getCode().equals(type)){
            task.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
            task.setImplementation(expression);
        }else if(ServiceTaskTypeEnum.EXPRESSION.getCode().equals(type)){
            task.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_EXPRESSION);
            task.setImplementation(expression);
            if(StringUtils.isNotEmpty(resultVariable)){
                task.setResultVariableName(resultVariable);
            }
        }else if(ServiceTaskTypeEnum.EXTERNAL_API.getCode().equals(type)){
            task.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_CLASS);
            task.setImplementation(SpringUtils.getBean(ExternalApiDelegate.class).getClass().getName());            ;
            String requestUrl  = BpmnJsonConverterUtil.getValueAsString("requestUrl", serviceConf);
            String requestBody  = BpmnJsonConverterUtil.getValueAsString("requestBody", serviceConf);
            List<FieldExtension> fieldExtensions=new ArrayList<>();
            FieldExtension apiAddress=new FieldExtension();
            apiAddress.setFieldName("requestUrl");
            apiAddress.setStringValue(requestUrl);
            fieldExtensions.add(apiAddress);
            FieldExtension apiBody=new FieldExtension();
            apiBody.setFieldName("requestBody");
            apiBody.setStringValue(requestBody);
            fieldExtensions.add(apiBody);
            FieldExtension apiVariable=new FieldExtension();
            apiVariable.setFieldName("resultVariable");
            apiVariable.setStringValue(resultVariable);
            fieldExtensions.add(apiVariable);

            task.setFieldExtensions(fieldExtensions);
        }
        if(StringUtils.isNotEmpty(failedJobRetryTimeCycleValue)){
            task.setFailedJobRetryTimeCycleValue(failedJobRetryTimeCycleValue);
        }

        return task;
    }

}
